<template>
  <div class="wrap">
    <div class="content" v-loading="loading">
      <el-form
        :model="currentForm"
        ref="form"
        label-width="150px"
        label-suffix=":"
        label-position="left"
        :rules="rules"
      >
        <div class="form-warehouseName">
          <el-form-item :label="$t('table.大仓库名')" label-width="100px" class="font-bold" prop="warehouseName">
            <el-input
              v-model="currentForm.warehouseName"
              autocomplete="off"
              style="width: 300px"
            ></el-input>
          </el-form-item>
        </div>


        <div class="form-box">
          <div class="form-title">{{ $t('setting.settingTitle3') }}：</div>
          <div class="header-tips">
            {{ $t('setting.以下数据为当前仓库默认数据，归属当前仓库的运营账号创建（绑定）的店铺（用户）适用以下单价') }}
            <br>
            {{ $t('setting.单独修改过单价的店铺（用户）不受以下默认单价控制，您可以通过运营端-用户列表中的「默认」按钮，将对应店铺（用户）恢复为“默认”') }}
          </div>

          <div class="form-content">
            <div class="price-box goods-size-price">
              <div class="platform mgb20">
                <el-radio-group v-model="currentAccountType" size="medium" class="time-radio-group">
                  <el-radio-button
                    v-for="accountItem in currentAccountTypeList"
                    :key="accountItem.accountType"
                    :label="accountItem.accountType"
                  >
                    {{accountItem.platformName}}
                    <template v-if="accountItem.isEdit">(已配置)</template>
                    <template v-else>(未配置)</template>
                  </el-radio-button>
                </el-radio-group>
              </div>
              <div class="price-title">{{$t('setting.一件代发服务')}}</div>
              <goods-size-price :goods-size-sticker-price="currentPlatformOneShipping.goodsSizeStickerPrice"></goods-size-price>


              <div class="price-title mgt20">{{$t('setting.settingLabel4')}}</div>
              <div class="price-content">

                <template v-if="currentAccountType === 'MercadoLibre'">
                  <div class="mgb20">
                    <h3>{{$t('快递（平台面单）')}}：</h3>
                    <goods-size-price :goods-size-sticker-price="currentPlatformOneShipping.logisticDropOffPrice"></goods-size-price>
                  </div>
                </template>

                <div class="mgb20">
                  <h3>{{$t('快递（无面单）')}}：</h3>
                  <el-form-item :label="$t('操作费')" label-width="100px">
                    <el-input-number
                      v-model="currentPlatformOneShipping.customPrice"
                      :precision="2"
                      :min="0"
                    >
                    </el-input-number>
                    {{$t('table.元/单')}}
                  </el-form-item>
                  <goods-size-price :goods-size-sticker-price="currentPlatformOneShipping.logisticCustomPrice"></goods-size-price>

                  <div class="append-price-tips mgt10">* {{$t('特殊类目平台不提供面单需快递发货时收取的操作费')}}</div>
                </div>

                <template v-if="currentAccountType === 'MercadoLibre'">
                  <div class="mgb20">
                    <h3>Flex：</h3>
                    <goods-size-price :goods-size-sticker-price="currentPlatformOneShipping.logisticSelfServicePrice"></goods-size-price>
                  </div>

                  <div class="mgb20">
                    <h3>Colecta：</h3>
                    <goods-size-price :goods-size-sticker-price="currentPlatformOneShipping.logisticCrossDockingPrice"></goods-size-price>
                  </div>
                </template>



              </div>



              <div class="append-price-tips">* {{$t('setting.settingText3')}}</div>

            </div>
          </div>


          <div class="form-content">
            <div class="price-box" style="width: 700px">
              <div class="price-title">{{$t('setting.settingLabel5')}}</div>
              <table class="table-content" style="text-align: left;border-spacing: 10px">
                <thead>
                <tr>
                  <th width="120">{{$t('setting.Packaging material')}}</th>
                  <th width="230">{{$t('setting.Foreground display name')}}</th>
                  <th>{{$t('setting.Unit price')}}</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="(item, index) in currentForm.packageMaterialSetting" :key="'package' + index">
                  <td style="margin-bottom: 10px">
                    {{$t('order.Packaging materials or Services')}}{{index+1}}:
                  </td>
                  <td>
                    <el-input style="width: 200px" size="small" placeholder="请输入内容" v-model="item.name"></el-input>
                  </td>
                  <td>
                    <el-input-number
                      v-model="item.price"
                      autocomplete="off"
                      :precision="2"
                      :min="0"
                    ></el-input-number>
                    <span style="margin-left: 5px">{{$t('setting.unit1')}}</span>
                  </td>
                  <td v-if="index !== 0">
                    <el-button style="padding: 2px" size="mini" type="danger" icon="el-icon-minus" circle @click="delPackageMaterialFun(index)"></el-button>
                  </td>
                </tr>
                </tbody>
              </table>
              <div class="mgt10">
                <el-button size="small" icon="el-icon-plus" type="primary" @click="AddPackageMaterialFun">{{$t('setting.Add more packing materials')}}</el-button>
              </div>
            </div>
          </div>

          <div class="form-content">
            <div class="middle-box">
              <div class="title">{{ $t('table.大货中转服务') }}</div>
              <div class="middle-content">
                <div class="middle-left">
                  <el-form-item :label="$t('table.送仓单价（常规）')" label-width="150px" >
                  </el-form-item>
                  <div class="middle-price-content mgb10 font-grey font-bold f14">
                    <div class="weight">{{$t('每箱实重或体积重')}}</div>
                    <div class="price">{{ $t('setting.Unit price') }}</div>
                  </div>
                  <div
                    class="middle-price-content mgb10 f14"
                    v-for="(item,index) in currentForm.middleGeneralPrice"
                    :key="'middleG' + index"
                  >
                    <div class="weight">
                      ≥
                      <el-input-number
                        v-model="item.weight"
                        :min="0"
                        :disabled="index === 0"
                        :precision="2"
                        controls-position="right"
                      ></el-input-number>
                      kg
                    </div>
                    <div class="price">
                      <el-input-number
                        v-model="item.price"
                        :min="0"
                        :precision="2"
                      ></el-input-number>
                      {{ $t('table.元/箱') }}
                      <div class="mgl10" style="display: inline">
                        <template v-if="index !== 0">
                          <el-button style="padding: 2px" type="danger" size="small" icon="el-icon-minus" circle @click="removeArrItemByIndex(currentForm.middleGeneralPrice,index)"></el-button>
                        </template>
                      </div>
                    </div>
                  </div>
                  <div class="mgt20">
                    <el-button icon="el-icon-plus" type="primary" @click="addMiddleGeneral(currentForm.middleGeneralPrice)">{{ $t('setting.添加更多重量范围') }}</el-button>
                  </div>

                  <el-form-item :label="$t('table.调货单价')" label-width="150px" class="mgt20"></el-form-item>
                  <div class="middle-price-content mgb10 font-grey font-bold f14">
                    <div class="weight">{{$t('每箱实重或体积重')}}</div>
                    <div class="price">{{ $t('setting.Unit price') }}</div>
                  </div>
                  <div
                    class="middle-price-content mgb10 f14"
                    v-for="(item,index) in currentForm.transferFeeList"
                    :key="'transfer' + index"
                  >
                    <div class="weight">
                      ≥
                      <el-input-number
                        v-model="item.weight"
                        :min="0"
                        :disabled="index === 0"
                        :precision="2"
                        controls-position="right"
                      ></el-input-number>
                      kg
                    </div>
                    <div class="price">
                      <el-input-number
                        v-model="item.price"
                        :min="0"
                        :precision="2"
                      ></el-input-number>
                      {{ $t('table.元/箱') }}
                      <div class="mgl10" style="display: inline">
                        <template v-if="index !== 0">
                          <el-button style="padding: 2px" type="danger" size="small" icon="el-icon-minus" circle @click="removeArrItemByIndex(currentForm.transferFeeList,index)"></el-button>
                        </template>
                      </div>
                    </div>
                  </div>
                  <div class="mgt20">
                    <el-button icon="el-icon-plus" type="primary" @click="addTransferFeeList(currentForm.transferFeeList)">{{ $t('setting.添加更多重量范围') }}</el-button>
                  </div>
                </div>
                <div class="middle-right">

                  <el-form-item :label="$t('table.送仓单价（快递）')" label-width="150px">
                  </el-form-item>
                  <div class="middle-price-content mgb10 font-grey font-bold f14">
                    <div class="weight">{{$t('每箱实重或体积重')}}</div>
                    <div class="price">{{ $t('setting.Unit price') }}</div>
                  </div>
                  <div
                    class="middle-price-content mgb10 f14"
                    v-for="(item,index) in currentForm.middleExpressPrice"
                    :key="'middleE' + index"
                  >
                    <div class="weight">
                      ≥
                      <el-input-number
                        v-model="item.weight"
                        :min="0"
                        :disabled="index === 0"
                        :precision="2"
                        controls-position="right"
                      ></el-input-number>
                      kg
                    </div>
                    <div class="price">
                      <el-input-number
                        v-model="item.price"
                        :min="0"
                        :precision="2"
                      ></el-input-number>
                      {{ $t('table.元/箱') }}
                      <div class="mgl10" style="display: inline">
                        <template v-if="index !== 0">
                          <el-button style="padding: 2px" type="danger" size="small" icon="el-icon-minus" circle @click="removeArrItemByIndex(currentForm.middleExpressPrice,index)"></el-button>
                        </template>
                      </div>
                    </div>
                  </div>
                  <div class="mgt20 mgb20">
                    <el-button icon="el-icon-plus" type="primary" @click="addMiddleGeneral(currentForm.middleExpressPrice)">{{ $t('setting.添加更多重量范围') }}</el-button>
                  </div>

                  <el-form-item :label="$t('table.贴SKU标单价')" label-width="150px">
                    <el-input-number
                      v-model="currentForm.goodsLabelFee"
                      autocomplete="off"
                      :min="0"
                      :precision="2"
                    ></el-input-number>
                    {{$t('table.元/个')}}
                  </el-form-item>
                  <el-form-item :label="$t('table.贴外箱标单价')" label-width="150px">
                    <el-input-number
                      v-model="currentForm.boxLabelFee"
                      autocomplete="off"
                      :min="0"
                      :precision="2"
                    ></el-input-number>
                    {{$t('table.元/个')}}
                  </el-form-item>
                  <el-form-item :label="$t('table.包车单价')" label-width="150px">
                    <el-input-number
                      v-model="currentForm.charteredCarFee"
                      autocomplete="off"
                      :min="0"
                      :precision="2"
                    ></el-input-number>
                    {{$t('table.元/单')}}
                  </el-form-item>

                </div>

              </div>
              <div class="mgt10 append-price-tips">
                * {{ $t('setting.送仓方式为包车或合并时按常规单价计算送仓费用，送仓方式为快递时按快递单价计算送仓费用') }}
                <br>
                * {{ $t('setting.每箱重量=单个商品的重量 * 对应箱装数') }}
              </div>
            </div>
          </div>

          <div class="form-content">
            <div class="tallying-box">
              <div class="title">{{$t('setting.理货服务')}}</div>
              <div class="tallying-content">
                <div class="tallying-left">
                  <el-form-item :label="$t('table.上架单价（一件代发）')" label-width="200px">
                  </el-form-item>
                  <div class="tallying-price-content mgb10 font-grey font-bold f14">
                    <div class="weight">{{$t('商品实重或体积重')}}</div>
                    <div class="price">{{ $t('setting.Unit price') }}</div>
                  </div>
                  <div
                    class="tallying-price-content mgb10 f14"
                    v-for="(item,index) in currentForm.shipmentOneWeightPrice"
                    :key="'shipmentOne' + index"
                  >
                    <div class="weight">
                      ≥
                      <el-input-number
                        v-model="item.weight"
                        :min="0"
                        :disabled="index === 0"
                        :precision="2"
                        controls-position="right"
                      ></el-input-number>
                      kg
                    </div>
                    <div class="price">
                      <el-input-number
                        v-model="item.price"
                        :min="0"
                        :precision="2"
                      ></el-input-number>
                      {{ $t('table.元/个') }}
                      <div class="mgl10" style="display: inline">
                        <template v-if="index !== 0">
                          <el-button style="padding: 2px" type="danger" size="small" icon="el-icon-minus" circle @click="removeArrItemByIndex(currentForm.shipmentOneWeightPrice,index)"></el-button>
                        </template>
                      </div>
                    </div>
                  </div>
                  <div class="mgt20">
                    <el-button icon="el-icon-plus" type="primary" @click="addShipmentOneWightPrice(currentForm.shipmentOneWeightPrice)">{{ $t('setting.添加更多重量范围') }}</el-button>
                  </div>
                </div>
                <div class="tallying-right">
                  <el-form-item label="上架单价（暂存中转）" label-width="200px">
                  </el-form-item>
                  <div class="tallying-price-content mgb10 font-grey font-bold f14">
                    <div class="weight">{{$t('每箱实重或体积重')}}</div>
                    <div class="price">{{ $t('setting.Unit price') }}</div>
                  </div>
                  <div
                    class="tallying-price-content mgb10 f14"
                    v-for="(item,index) in currentForm.shipmentMiddleWeightPrice"
                    :key="'shipmentMiddle' + index"
                  >
                    <div class="weight">
                      ≥
                      <el-input-number
                        v-model="item.weight"
                        :min="0"
                        :disabled="index === 0"
                        :precision="2"
                        controls-position="right"
                      ></el-input-number>
                      kg
                    </div>
                    <div class="price">
                      <el-input-number
                        v-model="item.price"
                        :min="0"
                        :precision="2"
                      ></el-input-number>
                      {{ $t('table.元/箱') }}
                      <div class="mgl10" style="display: inline">
                        <template v-if="index !== 0">
                          <el-button style="padding: 2px" type="danger" size="small" icon="el-icon-minus" circle @click="removeArrItemByIndex(currentForm.shipmentMiddleWeightPrice,index)"></el-button>
                        </template>
                      </div>
                    </div>
                  </div>
                  <div class="mgt20">
                    <el-button icon="el-icon-plus" type="primary" @click="addShipmentMiddleWeightPrice(currentForm.shipmentMiddleWeightPrice)">{{ $t('setting.添加更多重量范围') }}</el-button>
                  </div>
                </div>
              </div>
              <div class="mgt10 append-price-tips">
                * 一件代发货件按商品数量计费，暂存中转货件按箱计费({{ $t('setting.每箱重量=单个商品的重量 * 对应箱装数') }})
              </div>
            </div>
          </div>

          <div class="form-content">
            <div class="tallying-box">
              <div class="title">移仓/退仓服务</div>
              <div class="tallying-content">
                <div class="tallying-left">
                  <el-form-item label="散货进仓" label-width="200px">
                  </el-form-item>
                  <div class="tallying-price-content mgb10 font-grey font-bold f14">
                    <div class="weight">{{$t('商品实重或体积重')}}</div>
                    <div class="price">{{ $t('setting.Unit price') }}</div>
                  </div>
                  <div
                    class="tallying-price-content mgb10 f14"
                    v-for="(item,index) in currentForm.bulkCargoWeightPrice"
                    :key="'bulkCargoWeightPrice' + index"
                  >
                    <div class="weight">
                      ≥
                      <el-input-number
                        v-model="item.weight"
                        :min="0"
                        :disabled="index === 0"
                        :precision="2"
                        controls-position="right"
                      ></el-input-number>
                      kg
                    </div>
                    <div class="price">
                      <el-input-number
                        v-model="item.price"
                        :min="0"
                        :precision="2"
                      ></el-input-number>
                      {{ $t('table.元/箱') }}
                      <div class="mgl10" style="display: inline">
                        <template v-if="index !== 0">
                          <el-button style="padding: 2px" type="danger" size="small" icon="el-icon-minus" circle @click="removeArrItemByIndex(currentForm.bulkCargoWeightPrice,index)"></el-button>
                        </template>
                      </div>
                    </div>
                  </div>
                  <div class="mgt20">
                    <el-button icon="el-icon-plus" type="primary" @click="addBulkItem(currentForm.bulkCargoWeightPrice)">{{ $t('setting.添加更多重量范围') }}</el-button>
                  </div>
                </div>
                <div class="tallying-right">
                  <el-form-item label="整柜进仓" label-width="200px">
                  </el-form-item>
                  <div class="tallying-price-content mgb10 font-grey font-bold f14">
                    <el-form-item label="整柜单价" label-width="100px">
                      <el-input-number v-model="currentForm.wholeCabinetPrice" :precision="0" :min="0"></el-input-number> 元/柜
                    </el-form-item>
                  </div>

                </div>
              </div>
            </div>
          </div>

          <div class="form-content">
            <div class="storage-box">
              <div class="title">{{$t('仓储服务')}}</div>
              <div class="storage-content">
                <el-form-item :label="$t('免仓期')">
                  <el-input-number
                    v-model="currentForm.freeStorageDay"
                    :precision="0"
                    :min="0"
                  ></el-input-number>
                  {{$t('天')}}
                </el-form-item>
                <!-- 暂时不需要 -->
                <el-form-item :label="$t('计费时间')" v-if="false">
                  <el-time-select
                    v-model="currentForm.calStorageTime"
                    :picker-options="{start: '00:00', end: '23:00', step: '01:00'}"
                    :placeholder="$t('common.请选择')"
                  >
                  </el-time-select>
                </el-form-item>

                <el-form-item :label="$t('仓储单价（一件代发）')" label-width="200px"></el-form-item>
                <storage-range :weight-range-arr="shipmentOneStorageRange" type="stock_num" :can-delete-all="true" weight-type="goods" :cal-type.sync="currentForm.storageCalType.stock_num"></storage-range>

                <el-form-item :label="$t('仓储单价（暂存中转）')" label-width="200px" class="mgt20"></el-form-item>
                <storage-range :weight-range-arr="middleStorageRange" type="middle_num" :can-delete-all="true" weight-type="box" :cal-type.sync="currentForm.storageCalType.middle_stock_num"></storage-range>


                <template>
                  <el-form-item label="仓储单价（快出库存）" label-width="200px" class="mgt20"></el-form-item>
                  <storage-range :weight-range-arr="fullStorageRange" type="full_num" :can-delete-all="true" weight-type="goods" :cal-type.sync="currentForm.storageCalType.full_stock_num"></storage-range>

                </template>

              </div>
            </div>
          </div>

          <div style="color: #FF0000;font-size: 13px" class="mgt10">
            {{ $t('setting.settingText4') }}
          </div>
        </div>


        <div class="form-box">
          <div class="form-title">{{$t('logistics.发货地址')}}：</div>

          <el-form-item :label="$t('logistics.发货人')" label-width="100px" class="font-bold" prop="personName">
            <el-input
              v-model="currentForm.personName"
              autocomplete="off"
              style="width: 300px"
            ></el-input>
          </el-form-item>

          <el-form-item :label="$t('logistics.联系电话')" label-width="100px" class="font-bold" prop="phoneNumber">
            <el-input
              v-model="currentForm.phoneNumber"
              autocomplete="off"
              style="width: 300px"
            ></el-input>
          </el-form-item>

          <el-form-item :label="$t('logistics.公司名称')" label-width="100px" class="font-bold" prop="companyName">
            <el-input
              v-model="currentForm.companyName"
              autocomplete="off"
              style="width: 300px"
            ></el-input>
          </el-form-item>

          <el-form-item :label="$t('logistics.城市')" label-width="100px" class="font-bold" prop="city">
            <el-input
              v-model="currentForm.city"
              autocomplete="off"
              style="width: 300px"
            ></el-input>
          </el-form-item>

          <el-form-item :label="$t('logistics.邮政编码')" label-width="100px" class="font-bold" prop="postalCode">
            <el-input
              v-model="currentForm.postalCode"
              autocomplete="off"
              style="width: 300px"
            ></el-input>
          </el-form-item>

          <el-form-item :label="$t('logistics.详细地址')" label-width="100px" class="font-bold" prop="streetLines">
            <el-input
              type="textarea"
              v-model="currentForm.streetLines"
              autocomplete="off"
              style="width: 300px"
            ></el-input>
          </el-form-item>

          <div style="font-size: 12px; color: #7F7F7F;">
            {{$t('logistics.仓库地址和联系人信息，用于快递发货订单自动购买面单')}}
          </div>
        </div>


        <div class="form-box">
          <div class="form-title">{{ $t('setting.组合商品设置') }}：</div>
          <el-form-item :label="$t('setting.组合商品')">
            <el-radio-group v-model="currentForm.isOpenCombinationGoods">
              <el-radio :label="0">{{ $t('setting.不允许') }}</el-radio>
              <el-radio :label="1">{{ $t('setting.允许组合') }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <div class="form-content">
            <span class="append-price-tips">{{ $t('setting.允许组合后用户可将多个SKU绑定为一个SKU进行销售出货') }}</span>
          </div>
        </div>


        <div class="form-box">
          <div class="form-title">{{$t('pay.Credit Setting')}}：</div>
          <el-form-item :label="$t('pay.Credit limit')" label-width="200px">
            <el-input-number
              v-model="currentForm.creditAmount"
              autocomplete="off"
              :min="0"
              :precision="2"
            ></el-input-number>
            {{$t('common.RMB')}}
          </el-form-item>
          <div class="font-red" style="font-size: 13px">
            {{$t('pay.CreditText1')}}
            <br>
            {{$t('pay.CreditText2')}}
          </div>
        </div>


        <div class="form-box">
          <div class="form-title">货件设置：</div>
          <el-form-item :label="$t('table.货箱类型')" style="margin-bottom: 10px">
            <el-radio-group v-model="currentForm.shipmentTableType">
              <el-radio :label="1">{{ $t('setting.整箱和混箱') }}</el-radio>
              <el-radio :label="2">{{ $t('setting.仅整箱') }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <div class="form-content mgb20">
            <span class="append-price-tips">{{ $t('setting.整箱为一个货件箱一个SKU，混箱支持一个货件箱多个SKU') }}</span>
          </div>
          <el-form-item label="入库流程" style="margin-bottom: 10px">
            <el-radio-group v-model="currentForm.shipmentSortType">
              <el-radio :label="1">无需接收分拣</el-radio>
              <el-radio :label="2">接收分拣</el-radio>
            </el-radio-group>
          </el-form-item>
          <div class="form-content">
            <span class="append-price-tips">选择接收分拣后，移仓货件需要“入库分拣”权限的运营账号接收并分拣后才能操作上架</span>
          </div>

        </div>

        <div class="form-box" v-if="this.$store.getters.siteId === 'MX'">
          <div class="form-title">时区设置：</div>
          <el-form-item :label="'时区'">
            <el-radio-group v-model="currentForm.timeSite">
              <el-radio :label="'MX'">墨西哥城时间：{{ this.$store.getters.getNowTime }}</el-radio>
              <el-radio :label="'US_CA'">美国加州时间：{{ this.$store.getters.getUsCaNowTime }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <div class="form-content">
            <span class="append-price-tips">该大仓库下所有运营、客户账号均使用所选的时区显示所有时间数据，建议根据仓库/客户店铺所在地区选择对应的时区</span>
          </div>
        </div>

        <el-button @click="back">{{$t('common.back')}}</el-button>
        <el-button type="primary" :disabled="disableButton" @click="saveData" :loading="saveLoading">{{ $t('common.confirm') }}</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import {deepCopy, getFalabellaName, getMercadoName, getShopeeName, getSheinName, getAmazonName, getTikTokName} from "@/utils";
import {getSetting, saveSetting} from "@/api/setting";
import {getAppName} from "@/utils/settings";
import {getWareHouseName} from "@/utils/common";
import GoodsSizePriceMixin from "@/views/setting/mixin/GoodsSizePriceMixin";
import MiddlePriceMixin from "@/views/setting/mixin/MiddlePriceMixin";
import ShipmentPriceMixin from "@/views/setting/mixin/ShipmentPriceMixin";
import TransferFeeListMixin from "@/views/setting/mixin/TransferFeeListMixin";
import {getWarehouseInfo, saveWarehouse} from "@/api/warehouse";
import LogisticTypePriceMixin from "@/views/setting/mixin/LogisticTypePriceMixin";
import GoodsSizePrice from "@/views/warehouse/components/GoodsSizePrice.vue";
import StorageWeightRange from "@/views/warehouse/components/StorageWeightRange.vue";
import StorageWeightRangeMixin from "@/views/warehouse/mixin/StorageWeightRangeMixin";
import BulkCargoWeightPriceMixin from "@/views/setting/mixin/BulkCargoWeightPriceMixin";
import StorageRange from "@/views/warehouse/components/StorageRange.vue";



const accountTypeList = [
  {
    accountType: 'MercadoLibre', // 平台字符
    platformName: '美客多', // 平台名
    site: ['MX','CL','BR'], // 显示的站点
  },
  {
    accountType: 'Falabella',
    platformName: 'Falabella',
    site: ['CL'],
  },
  {
    accountType: 'Shein',
    platformName: '希音',
    site: ['MX'],
  },
  {
    accountType: 'TikTok',
    platformName: 'Tiktok',
    site: ['MX','CL','BR'],
  },
  {
    accountType: 'Packet',
    platformName: '小包',
    site: ['MX','CL','BR'],
  },
]

export default {
  name: "index",
  components: {StorageRange, GoodsSizePrice},
  mixins: [GoodsSizePriceMixin, MiddlePriceMixin, ShipmentPriceMixin,TransferFeeListMixin, LogisticTypePriceMixin, StorageWeightRangeMixin, BulkCargoWeightPriceMixin],
  data(){
    return {
      currentForm: {
        id: 0,
        stickerPrice: 0,
        storagePrice1: 0,
        storagePrice2: 0,
        stickerAppend: 0,
        stockTipsNum: 0,
        logisticTypePrice: {
          drop_off: 0, // 快递
          self_service: 0, // Flex
          cross_docking: 0, // Colecta
        }, // 弃用了
        logisticDropOffPrice: [], // 快递(平台)
        logisticCustomPrice: [], // 快递(无面单)
        logisticSelfServicePrice: [], // Flew
        logisticCrossDockingPrice: [], // Colecta
        customPrice: 0, // 操作费
        packagePrice: 0, // 包装服务
        packageMaterialSetting: [], // 包装服务(新)
        untreatedOrderScreenTime: '',
        creditAmount: 0,
        transfer2Warehouse: 0, // 中转送仓
        goodsLabelFee: 0, // 贴换标费用
        boxLabelFee: 0, // 贴外箱标价格
        warehouseName: '', // 仓库名
        shipmentTableType: 1, // 表单类型
        goodsSizeStickerPrice: [], // 商品体积重价格
        charteredCarFee: 0, // 包车费用
        middleGeneralPrice: [], // 送仓单价(常规),
        middleExpressPrice: [], // 送仓单价(快递),
        shipmentOneWeightPrice: [], // 一件代发 上架单价
        shipmentMiddleWeightPrice: [], // 中转送仓 上架单价
        transferFeeList: [], // 调货单价 （箱）
        personName: '', // 发货人
        phoneNumber: '', // 联系方式
        companyName: '', // 公司名称
        city: '', // 城市
        postalCode: '', // 邮政编码
        streetLines: '', // 详细地址
        freeStorageDay: 0, // 免仓期
        calStorageTime: '', // 计算仓储费时间
        shipmentOneStorageRange: [], // 一件代发仓储
        shipmentOneStorageVolumeRange: [], // 一件代发仓储
        middleStorageRange: [], // 中转送仓仓储
        middleStorageVolumeRange: [], // 中转送仓仓储
        fullStorageRange: [], // 快出仓储
        fullStorageVolumeRange: [], //
        platformOneShippingPrice: [], // 各个平台的一件代发价格
        bulkCargoWeightPrice: [], // 散货进仓
        wholeCabinetPrice: 0, // 整柜进仓
        timeSite: this.$store.getters.siteId,
        shipmentSortType: 1, // 上架流程
        storageCalType: {
          full_stock_num: 'weight',
          middle_stock_num: 'weight',
          stock_num: 'weight',
        },
      },
      copyForm: {}, // 用于对比是否有数据变化
      loading: false,
      saveLoading: false,
      packageMaterialItem:{
        id:0,
        name:'',
        price:0,
        is_del:0,
      },
      defaultPackageNameItem: ['快递袋','快递箱'],
      rules:{
        warehouseName: [
          { required: true, message:this.$t('common.pleaseFillIn'), trigger: 'blur' },
        ],
        // personName: [
        //   { required: true, message:this.$t('common.pleaseFillIn'), trigger: 'blur' },
        // ],
        // phoneNumber: [
        //   { required: true, message:this.$t('common.pleaseFillIn'), trigger: 'blur' },
        // ],
        // companyName: [
        //   { required: true, message:this.$t('common.pleaseFillIn'), trigger: 'blur' },
        // ],
        // city: [
        //   { required: true, message:this.$t('common.pleaseFillIn'), trigger: 'blur' },
        // ],
        // postalCode: [
        //   { required: true, message:this.$t('common.pleaseFillIn'), trigger: 'blur' },
        // ],
        // streetLines: [
        //   { required: true, message:this.$t('common.pleaseFillIn'), trigger: 'blur' },
        // ],
      },
      accountTypeList, // 所有平台的类型
      currentAccountType: '', // 当前选择的平台
    }
  },
  created() {
    this.currentForm.goodsSizeStickerPrice.push(deepCopy(this.defaultGoodsSizeStickerPrice))
    this.currentForm.middleGeneralPrice.push(deepCopy(this.defaultMiddleGeneral))
    this.currentForm.middleExpressPrice.push(deepCopy(this.defaultMiddleExpress))
    this.addShipmentOneWightPrice(this.currentForm.shipmentOneWeightPrice)
    this.addShipmentMiddleWeightPrice(this.currentForm.shipmentMiddleWeightPrice)
    this.addTransferFeeList(this.currentForm.transferFeeList)


    this.addRange(this.currentForm.shipmentOneStorageRange)
    this.addRange(this.currentForm.middleStorageRange)

    // 特殊快递
    // this.addLogisticPrice(this.currentForm.logisticDropOffPrice)
    // this.addLogisticPrice(this.currentForm.logisticSelfServicePrice)
    // this.addLogisticPrice(this.currentForm.logisticCrossDockingPrice)
    // this.addLogisticPrice(this.currentForm.logisticCustomPrice)

    // 默认包装服务
    this.currentForm.packageMaterialSetting = this.defaultPackageNameItem.map(item=>{
      const obj = deepCopy(this.packageMaterialItem)
      obj.name = item
      return obj
    })

    this.currentAccountType = this.currentAccountTypeList[0].accountType

    // 设置默认的数值
    this.defaultPlatformOneShippingPrice()

    this.currentForm.id = this.$route.query.id
    this.copyForm = deepCopy(this.currentForm)
    this.getData();
  },
  computed:{
    disableButton(){
      return JSON.stringify(this.currentForm) === JSON.stringify(this.copyForm)
    },
    // 当前显示的平台 需要通过站点过滤
    currentAccountTypeList(){
      return this.accountTypeList.filter(item => item.site.includes(this.$store.getters.siteId))
        .map(item=>{
          const platformPrice = this.currentForm.platformOneShippingPrice.find(_item => item.accountType === _item.accountType)
          item.isEdit = true; // 是否有编辑过
          if(platformPrice){
            const flag = platformPrice.customPrice !== 0 || this.checkIsEdit(platformPrice.goodsSizeStickerPrice)
              || this.checkIsEdit(platformPrice.logisticDropOffPrice)
              || this.checkIsEdit(platformPrice.logisticSelfServicePrice)
              || this.checkIsEdit(platformPrice.logisticCrossDockingPrice)
              || this.checkIsEdit(platformPrice.logisticCustomPrice)
            item.isEdit = flag
          }

          return item
        })
    },
    // 当前所选平台的一件代发
    currentPlatformOneShipping(){
      const curr = this.currentForm.platformOneShippingPrice.find(item => item.accountType === this.currentAccountType)
      return curr ? curr : {}
    },
    // 仓储费（一件代发）
    shipmentOneStorageRange(){
      if(this.currentForm.storageCalType.stock_num === 'weight'){
        return this.currentForm.shipmentOneStorageRange
      }else{
        return this.currentForm.shipmentOneStorageVolumeRange
      }
    },
    // 仓储费（暂存中转）
    middleStorageRange(){
      if(this.currentForm.storageCalType.middle_stock_num === 'weight'){
        return this.currentForm.middleStorageRange
      }else{
        return this.currentForm.middleStorageVolumeRange
      }
    },
    // 仓储费（快出库存）
    fullStorageRange(){
      if(this.currentForm.storageCalType.full_stock_num === 'weight'){
        return this.currentForm.fullStorageRange
      }else{
        return this.currentForm.fullStorageVolumeRange
      }
    },


  },
  methods: {
    objEquals(obj1, obj2){
      return JSON.stringify(obj1) === JSON.stringify(obj2)
    },
    checkIsEdit(item){
      if(item.length !== 1){
        return true
      }
      const row = item[0];
      return row.PSCFirst !== 0 || row.PSCAppend !== 0 || row.weightFirst !== 0 || row.weightAppend !==0
    },
    getData(){
      if(!this.currentForm.id){
        return
      }
      this.loading = true;
      getWarehouseInfo(this.currentForm.id)
        .then(res=>{
          this.loading = false

          this.currentForm = deepCopy(res.data)
          this.copyForm = deepCopy(this.currentForm)
          this.defaultPlatformOneShippingPrice()

          if (this.currentForm.timeSite === '') {
            this.currentForm.timeSite = this.$store.getters.siteId
          }
        })
    },
    // 删除包材
    delPackageMaterialFun(index){
      let data = this.currentForm.packageMaterialSetting[index]
      if(data.id !== 0){
        if(data.settingNum > 0){
          this.$confirm(this.$t('setting.DelPackageMaterialFun Tis One') + '<br>' + this.$t('setting.DelPackageMaterialFun Tis Two') + '<br><span style="color: red">' + this.$t('setting.DelPackageMaterialFun Tis Three') + '</span>', this.$t('setting.DelPackageMaterialFun Tis Four'), {
            confirmButtonText: this.$t('common.confirm'),
            cancelButtonText: this.$t('common.cancel'),
            dangerouslyUseHTMLString: true
          }).then(() => {
            this.currentForm.packageMaterialSetting.splice(index, 1)
          }).catch(res=>{
            // 点击了取消
          });
        }else{
          this.currentForm.packageMaterialSetting.splice(index, 1)
        }
      }else{
        this.currentForm.packageMaterialSetting.splice(index, 1)
      }
    },
    AddPackageMaterialFun(){
      let item = deepCopy(this.packageMaterialItem)
      this.currentForm.packageMaterialSetting.push(item);
    },
    saveData(){

      let check = !this.checkMiddleGeneral(this.currentForm.middleGeneralPrice) ||
        !this.checkMiddleExpress(this.currentForm.middleExpressPrice) ||
        !this.checkShipmentOneWeightPrice(this.currentForm.shipmentOneWeightPrice) ||
        !this.checkShipmentMiddleWeightPrice(this.currentForm.shipmentMiddleWeightPrice) ||
        !this.checkTransferFeeList(this.currentForm.transferFeeList) ||
        !this.checkBulkCargoWeightPrice(this.currentForm.bulkCargoWeightPrice) ||
        !this.checkRange(this.shipmentOneStorageRange) ||
        !this.checkRange(this.middleStorageRange) ||
        !this.checkRange(this.fullStorageRange)

      // 校验相关数据
      if(check){
        return;
      }

      if(this.currentForm.warehouseName === ''){
        this.$errorMessage('请填写仓库名');
        return;
      }

      this.$refs.form.validate(valid=>{
        if(valid){
          this.saveLoading = true;
          saveWarehouse(this.currentForm)
            .then(res=>{
              this.saveLoading = false;
              if(res.error === 0){
                this.$successMessage(res.message)
                if(!this.currentForm.id){
                  this.copyForm = deepCopy(this.currentForm)
                  this.back();
                }else{
                  this.getData()
                }
              }else{
                this.$errorMessage(res.message)
              }
            })
        }
      })
    },
    back(){
      this.$router.go(-1)
    },
    defaultPlatformOneShippingPrice(){
      this.currentAccountTypeList.forEach(accountTypeItem => {
        if(!this.currentForm.platformOneShippingPrice.find(item => item.accountType === accountTypeItem.accountType)){
          const obj = {
            accountType: accountTypeItem.accountType,
            goodsSizeStickerPrice: [],
            logisticDropOffPrice: [],
            logisticSelfServicePrice: [],
            logisticCrossDockingPrice: [],
            logisticCustomPrice: [],
            customPrice: 0,
          }
          this.addGoodsSizePrice(obj.goodsSizeStickerPrice)
          this.addLogisticPrice(obj.logisticDropOffPrice)
          this.addLogisticPrice(obj.logisticSelfServicePrice)
          this.addLogisticPrice(obj.logisticCrossDockingPrice)
          this.addLogisticPrice(obj.logisticCustomPrice)
          this.currentForm.platformOneShippingPrice.push(obj)
        }
      })
    }
  },
  // 路由跳转离开提示
  beforeRouteLeave(to,from,next){
    if(!this.disableButton){
      setTimeout(()=>{
        this.$warning('',{
          title: this.$t('setting.当前信息未保存，确认离开？'),
        }).then(()=>{
          next();
        }).catch(() => {
          next(false);
        })
      },0)
    }else{
      next()
    }
  },
}
</script>
<style>
.form-box{
  .el-form-item__label{
    color: #000000;
    font-weight: bold;
    font-size: 15px;
  }
}
.form-warehouseName{
  .el-form-item__label{
    color: #000000;
    font-weight: bold;
    font-size: 15px;
  }
}
</style>

<style scoped lang="scss">
.wrap {
  padding: 10px 30px;
  .header{
    display: flex;
    justify-content: space-between;
  }
  .content{
    padding: 20px 0 0;
    .header-tips{
      font-size: 14px;
    }
    .form{
      margin-top: 30px;
    }
  }
  .form-box{
    border-radius: 2px;
    border: 1px solid #f2f2f2;
    padding: 15px 20px;
    margin-bottom: 20px;
    .form-title{
      font-weight: bold;
      font-size: 18px;
      //color: #ff0000;
      margin-bottom: 10px;
    }
    .header-tips{
      font-weight: 500;
      color: #FF0000;
    }
    .form-content{
      display: flex;
      flex-direction: row;
      align-items: flex-start;
      .price-box{
        border-radius: 2px;
        border: 1px solid #f2f2f2;
        padding: 15px 20px;
        width: 500px;
        margin-top: 20px;
        margin-right: 50px;
        .price-title{
          font-weight: bold;
          font-size: 14px;
          color: #409EFF;
          margin-bottom: 10px;
        }
      }
      .middle-box{
        border-radius: 2px;
        border: 1px solid #f2f2f2;
        padding: 15px 20px;
        width: 1200px;
        margin-top: 20px;
        margin-right: 50px;
        .title{
          font-weight: bold;
          font-size: 14px;
          color: #409EFF;
          margin-bottom: 10px;
        }
        .middle-content{
          display: flex;
          flex-direction: row;
          justify-content: space-between;
          .middle-left{
            width: 50%;
          }
          .middle-right{
            width: 50%;
          }
        }
        .middle-price-content{
          display: flex;
          flex-direction: row;
          justify-content: space-between;
          .weight{
            width: 50%;
          }
          .price{
            width: 50%;
          }
        }

      }
      .tallying-box{
        border-radius: 2px;
        border: 1px solid #f2f2f2;
        padding: 15px 20px;
        width: 1200px;
        margin-top: 20px;
        margin-right: 50px;
        .title{
          font-weight: bold;
          font-size: 14px;
          color: #409EFF;
          margin-bottom: 10px;
        }
        .tallying-content{
          display: flex;
          flex-direction: row;
          justify-content: space-between;
          .tallying-left{
            width: 50%;
          }
          .tallying-right{
            width: 50%;
          }
          .tallying-price-content{
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            .weight{
              width: 50%;
            }
            .price{
              width: 50%;
            }
          }
        }
      }
      .goods-size-price{
        width: 1000px;
        .goods-size-price-content{
          display: flex;
          flex-direction: row;
          margin-bottom: 10px;
          align-items: center;
          justify-content: flex-start;
          .weight{
            width: 220px;
          }
          .cal-type{
            width: 180px;
          }
          .PSC-first{
            width: 200px;
          }
          .PSC-append{
            width: 200px;
          }
          .remove-item{
            width: 50px;
          }
        }
      }
      .logistic-type{
        width: 1000px;
        .logistic-type-content{
          display: flex;
          flex-direction: row;
          justify-content: space-between;
          margin-bottom: 10px;
          font-size: 14px;
          color: #7f7f7f;
          font-weight: bold;
          .weight{
            width: 200px;
          }
          .PSC-first{
            width: 200px;
          }
          .PSC-append{
            width: 250px;
          }
        }
      }
      .append-price-tips{
        font-size: 12px;
        color: #7F7F7F;
      }
      .storage-box{
        border-radius: 2px;
        border: 1px solid #f2f2f2;
        padding: 15px 20px;
        width: 1000px;
        margin-top: 20px;
        .title{
          font-weight: bold;
          font-size: 14px;
          color: #409EFF;
          margin-bottom: 10px;
        }
        .storage-content{

        }
      }
    }
  }
}

</style>
